package com.slwish.xty.mvp.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.CommentLabelModel;
import com.slwish.xty.mvp.contract.CommentEditContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * 首页
 *
 * @author 陈浩
 * @version 1.0.0
 */
public class CommentEditPresenter extends BasePresenter<CommentEditContract.View> implements CommentEditContract.Presenter {

    public CommentEditPresenter(CommentEditContract.View view) {
        // 初始化数据仓库
        super(view);
    }

    /**
     * 登录
     */
    @Override
    public void queryCommentLabel() {

        EasyHttp.post(UrlConfig.QUERY_COMMENT_LABEL_URL)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        setError(R.string.loading_comment_tabel_exception);
                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json:" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            String data = jsonObject.getString("data");
                            List<CommentLabelModel> model = JSON.parseArray(data, CommentLabelModel.class);
                            if (model != null && getView() != null) {
                                getView().queryCommentLabelSuccess(model);
                            }
                        } catch (Exception e) {
                            setError(R.string.loading_comment_tabel_exception);
                        }
                    }
                });
    }

    /**
     * 新增评论
     *
     * @param orderId   订单号
     * @param aid       用户id
     * @param message   评价简述
     * @param content   具体评价内容
     * @param commLabel 评论标签
     * @param appraiser 被评价商家
     * @param grade     评论等级  不必须传
     */
    @Override
    public void addComment(String orderId, long aid, String message, String content, String commLabel, String appraiser, String grade) {
        EasyHttp.post(UrlConfig.ADD_COMMENT_URL)
                .params("orderId", orderId)
                .params("aid", aid + "")
                .params("message", message)
                .params("content", content)
                .params("commLabel", commLabel)
                .params("appraiser", appraiser)
                .params("grade", grade)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        setError(R.string.input_failed_text);
                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json:" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0"))
                                return;

//                            String data = jsonObject.getString("data");
//                            List<CommentLabelModel> model = JSON.parseArray(data, CommentLabelModel.class);
//                            if (model != null && getView() != null) {
//                                getView().queryCommentLabelSuccess(model);
//                            }
                        } catch (Exception e) {
                            setError(R.string.input_failed_text);
                        }
                    }
                });
    }
}
